<?php

namespace app\forensic\model;

use think\Model;
use think\Exception;
use app\forensic\controller\Sample as SampleC;

class DNACase extends Model
{
    protected $table = 'forensic_case';

    /**
     * @param mixed $value
     * @return mixed
     * @throws Exception
     */
    public function setCaseIdAttr($value)
    {
        if ($value=='' || !$value) throw new Exception('未设置案件编号');
//        $exist=$this->where(['case_id'=>$value])->select();
//        if (count($exist)>0) throw new Exception('案件'.$value.'已存在');
        return $value;
    }

    /**
     * @param mixed $value
     * @return mixed
     * @throws Exception
     */
    //验证案件类型
    public function setCaseTypeAttr($value)
    {
//        var_dump($value);
        $arr_type=['2l','3l','ty'];
        if (in_array($value,$arr_type)){
            return $value;
        }else{
            throw new Exception('案件类型错误！');
        }
    }

    /**
     * @param mixed $value
     * @param mixed $data
     * @return mixed
     * @throws \Exception
     * 自动格式化样品数据并存储
     * 前端写法：
     * sample_name_F sample_type_F
     * person_name_C person_sex_C person_race_C person_id_C
     */
    public function setSamplesAttr($value, $data)
    {
//        var_dump($data);
        //提取样品、人员数据
        $sample_need=['sample_name','sample_type','sample_fob','person_name','person_id','person_sex','person_race'];
        $sample=null;
        $index_sample=[];
        foreach ($data as $key=>$val){
            foreach ($sample_need as $need) {
                if (preg_match('/^'.$need.'/',$key)){
                    $role_sample=str_replace($need.'_','',$key); //获取样品所属角色
                    $sample[$role_sample][$need]=$val;
                    unset($data[$key]);
                    if($need=='sample_name'){
                        $index_sample[$role_sample]=$val;
                    }
                }
            }
        }
        // throw new Exception(var_dump($data));
        //验证数据
        $type=$data['case_type'];
        //TODO:去掉数量限制
        $rule=[
            '2l'=>['F','M','C','C0','C1','C2','C3'],
            '3l'=>['F','M','C','C0','C1','C2','C3'],
            'ty'=>['yz','yz0','yz1','yz2','wz','wz0','wz1','wz2','wz3','wz4'],
        ];
        $role_sample=array_keys($sample);
        foreach ($role_sample as $item) {
            if (!in_array($item,$rule[$type])) throw new Exception('样品对象角色设置错误');
        }
        //存储样品数据
        $model_sample=new Sample();
        $sampleArr=[];
        foreach ($sample as $item){//存储样品数据
            $a=$model_sample->where(['sample_name'=>$item['sample_name']])->select();
            if (count($a)>0){
                throw new Exception('样品'.$item['sample_name'].'已存在');
            }else{
                $sampleArr[]=$item;
            }
        }
        $model_sample->allowField(true)->saveAll($sampleArr);
        return serialize($index_sample);
    }

    //samples获取器,将样品和人员信息全部加入到samples数组
    public function getSamplesAttr($value)
    {
        $sample=[];
            $origin = unserialize($value);
            if (is_array($origin)){
                foreach ($origin as $index0 => $sampleName) {
                    $data_sample = Sample::get(['sample_name'=>$sampleName]);
                    if ($data_sample instanceof Sample){
//                        $data_person = Person::get(['id_sample'=>$data_sample->id]);
                        $arr_sample= $data_sample ? $data_sample->toArray() : [];
//                        $arr_person= $data_person ? $data_person->toArray() : [];
                        $sample[$index0]=$arr_sample;
                    }
                    unset($sample[$index0]['id']);
                    unset($sample[$index0]['id_sample']);
                    unset($sample[$index0]['create_time']);
                    unset($sample[$index0]['update_time']);
                }
                return $sample;
            }else{
                return $value;
            }
    }

    //委托方类型
    public function setClientTypeAttr($value)
    {
        if (in_array($value,[0,1])){
            return $value;
        }else{
            throw new Exception('委托方类型“'.$value.'”错误！');
        }
    }

    // public function getClientTypeAttr($value)
    // {
    //     $type=[0=>'委 托 人',1=>'委托单位'];
    //     return $type[$value];
    // }

    //委托日期
    public function setRequestDateAttr($value)
    {
        if ($a=strtotime($value)){
            return $a;
        }else{
            throw new Exception('委托日期格式错误');
        }
    }
    public function getRequestDateAttr($value)
    {
        return date('Y-m-d',$value);
    }

    //受理日期
    public function setAcceptDateAttr($value)
    {
        if ($a=strtotime($value)){
            return $a;
        }else{
            throw new Exception('受理日期格式错误');
        }
    }
    public function getAcceptDateAttr($value)
    {
        return date('Y-m-d',$value);
    }

    //报告日期
    public function setEndDateAttr($value)
    {
        if ($a=strtotime($value)){
            return $a;
        }else{
            throw new Exception('报告日期格式错误');
        }
    }
    public function getEndDateAttr($value)
    {
        return date('Y-m-d',$value);
    }


}